console.log('This is my first typescript application.');
class User {
    name: string;
    id: number;
    constructor(id: number, name: string) {
        this.name = name;
        this.id = id;
    }
    showName() {
        console.log(this.name);
    }
}

class Role {
    name: string;
    id: number;
    constructor() { }
}

function getById(id: number) {
    let user = new User(id, "gainorloss");
    return user;
}

var role = new Role();
role.id = 1;
role.name = '管理员';

console.log(role.id)


let registered: boolean = true;
var str: string = "gainorloss";
var num: number = 1;
let strArr: string[] = ["gain", "loss", "or"];
let strArr_: Array<string> = ["gain", "or", "loss"];
let tuple: [string, number] = ["gain", 3, "string", 4, 6, 7];
enum Color {
    Red = 6,
    Yellow,
    Green,
    Blue
}
console.log(Color.Yellow);

var obj: any = 'string';
obj = false;
obj = 123;
console.log(obj);

var un: string | undefined | null;
console.log(un);

function createUser(): void {
    console.log("create user.");
}

var oBtn: any = document.getElementById('btn');
console.log(oBtn.innerText = "Please click me");

function getName(name?: string, age: number = 25) {
    if (name) {
        console.log(`${name}___${age}`);
        return;
    }
    console.log(`${age}`);
}

getName("gainorloss", 26);

function add(left: number, ...para: number[]) {
    var sum = left;
    for (var i in para) {
        sum += para[i];
    }
    return sum;
}
var rt = add(1, 2, 3, 4, 5);
console.log(rt);

function getString(str: string): string;
function getString(num: number): string;
function getString(is: boolean): string;
function getString(any: any): string {
    if (typeof any === 'string') {
        return `string___${any}`;
    } else {
        return `${typeof any}___${num.toString()}`;
    }
}
console.log(getString(true));
class Person {
    protected name: string;
    protected age: number;
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    sayHello(): void {
        console.log(`${this.name}:hello!`);
    }
}

class Chinese extends Person {
    constructor(name: string, age: number) {
        super(name, age)
    }
}

var chinese = new Chinese("gainorloss", 26);
chinese.sayHello();

class Animal {
    name: string;
    age: number;
    static nickName: string = 'wildLife';
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    eat() {
        console.log(`${this.name} is eating!`);
    }
    static run() {
        console.log(`Animal is eating!`);
    }
}

console.log(Animal.nickName);
Animal.run()


abstract class Life {
    type: string;
    constructor(type: string) {
        this.type = type;
    }
   abstract run():void;
}

class WildLife extends Life{
    constructor(type:string){
        super(type);
    }
    run(){
        console.log('WildLife run');
    }
}

var life=new WildLife("豹子");
life.run();

interface FullName{
    firstName:string;
    lastName:string;
}
function getFullName(fullName:FullName){
    console.log(`${fullName.firstName}${fullName.lastName}`);
}
var fullName={firstName:'张',lastName:'三'};
getFullName(fullName);
interface Config{
    type:string;
    url:string;
    data?:string;
    dataType:string;
}
function ajax(config:Config){
    var xhr=new XMLHttpRequest();
    xhr.open(config.type,config.url,true);
    xhr.send(config.data);
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4&&xhr.status==200){
              if(config.dataType=='json'){
                  console.log(JSON.parse(xhr.responseText));
              }else {
                console.log(xhr.responseText);
              }
        }
    }
}

interface IDoSomething{
    (firstName:string,lastName:string):string;
}

var doSomething:IDoSomething=function(firstName:string,lastName:string){
return `${firstName} ${lastName}`;
};

console.log(doSomething('张','建'));


interface ISayHello{
    SayHello(message:string):void;
}

class People implements ISayHello{
     SayHello(message:string):void{
         console.log(message);
     }
}

interface IWork{
    work():void;
}

class Human{
    name:string;
    constructor(name:string){
        this.name=name;
    }
    eat(){
        console.log(`${this.name} is eating!`);
    }
}
class Americal extends Human implements IWork
{
    constructor(name:string){
        super(name);
    }
   work():void{
   console.log(`${this.name} is working.`);
   }
}  


function get<T>(val:T){
    return val;
}

var str=get<string>("gainorloss");

var num=get<number>(123);

var boo=get<boolean>(true);

class MyClassName<T>{
    get(val:T):T{
        return val;
    }
}
interface IMyInterface{
    getValue<T>(val:T):T;
}
var myClassName=new MyClassName<string>();
var string=myClassName.get("gainorloss");